from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
import random
# 创建AcsClient实例，需替换为您自己的AccessKey信息
def send_code(phone):
    ACCESS_KEY_ID = 'LTAI5t7GXRADnL2rj3gUfXf9'
    ACCESS_KEY_SECRET = 'KP5mBu4vtSWH8dnEI1NIl1LUqjTE09'
    SIGN_NAME = '阿里云短信测试'  # 短信签名
    template_code = 'SMS_154950909'  # 短信模板CODE
    PhoneNumber = '17763438143'  # 绑定的测试手机号
    acs_client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, template_code)

    # 创建CommonRequest实例
    request = CommonRequest()
    request.set_accept_format('json')  # 设置API响应格式的方法
    request.set_domain('dysmsapi.aliyuncs.com')  # 设置API的域名的方法
    request.set_method('POST')  # 设置API请求方法
    request.set_version('2017-05-25')  # 设置API版本号
    request.set_action_name('SendSms')  # 设置API操作名
    r_code = random.randint(000000, 999999)
    # 设置短信模板参数
    request.add_query_param('PhoneNumbers', PhoneNumber)
    request.add_query_param('SignName', SIGN_NAME)
    request.add_query_param('TemplateCode', template_code)
    request.add_query_param('TemplateParam', {"code": r_code})

    # 发送短信请求并获取返回结果
    response = acs_client.do_action_with_exception(request)

    print(response.decode('utf-8'))
